$(function () {
    $('#keyword').on('keyup',function (){
        var keyword = $(this).val();
        console.log(keyword);
        if(keyword !== '' && keyword !== null && keyword.length !== 0){
            $.ajax({
                type :'GET',
                url :'http://localhost:8090/auto/complete?keyword='+keyword,
                success :function (data){
                    // console.log(data);
                    var productListHTML = '';
                    for (var i = 0; i < data.length; i++){
                        productListHTML += '<li class=\"productAutoItem\" data-productId="';
                        productListHTML += data[i].productId;
                        productListHTML += '">';
                        productListHTML += data[i].categoryId;
                        productListHTML += ': ';
                        productListHTML += data[i].name;
                        productListHTML += '</li>';
                    }
                    $('#drop').html(productListHTML);
                    $('#selectedId').show();
                },
                error:function (errorMsg){
                    console.log(errorMsg);
                }
            });
        }else {
            $('#selectedId').hide();
        }
    });

    $(document).on('click','.productAutoItem',function (){
        var productId = $(this).data('productid');
        $('#selectedId').hide();
        $('#keyword').val('');
        window.location.href = 'http://localhost:8090/productForm?productId='+productId;
    });

    $('#selectedId').on('mouseleave', function (){
        $(this).hide();
        $('#keyword').val('');
    })
})